package com.ruoyi.web.controller.sts;

import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sts.domain.StsDevice;
import com.ruoyi.sts.service.IStsDeviceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author maoxiaodong
 * @date 2025-04-24
 */
@Controller
@RequestMapping("/sts/device")
public class StsDeviceController extends BaseController
{
    private String prefix = "sts/device";

    @Autowired
    private IStsDeviceService stsDeviceService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("sts:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备列表
     */
    @RequiresPermissions("sts:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StsDevice stsDevice)
    {
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        if(sysDept != null){
            String [] ancestors =  sysDept.getAncestors().split(",");
            if(ancestors.length > 1){
                stsDevice.setDeptId1(Long.valueOf(ancestors[1]));
            }else{
                stsDevice.setDeptId1(deptId);
            }

        }
        startPage();
        List<StsDevice> list = stsDeviceService.selectStsDeviceList(stsDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @RequiresPermissions("sts:device:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StsDevice stsDevice)
    {
        List<StsDevice> list = stsDeviceService.selectStsDeviceList(stsDevice);
        ExcelUtil<StsDevice> util = new ExcelUtil<StsDevice>(StsDevice.class);
        return util.exportExcel(list, "设备数据");
    }

    /**
     * 新增设备
     */
    @RequiresPermissions("sts:device:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @RequiresPermissions("sts:device:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StsDevice stsDevice)
    {
        return toAjax(stsDeviceService.insertStsDevice(stsDevice));
    }

    /**
     * 修改设备
     */
    @RequiresPermissions("sts:device:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StsDevice stsDevice = stsDeviceService.selectStsDeviceById(id);
        mmap.put("stsDevice", stsDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备
     */
    @RequiresPermissions("sts:device:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StsDevice stsDevice)
    {
        return toAjax(stsDeviceService.updateStsDevice(stsDevice));
    }

    /**
     * 删除设备
     */
    @RequiresPermissions("sts:device:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stsDeviceService.deleteStsDeviceByIds(ids));
    }


    @GetMapping("/deptTreeData")
    @ResponseBody
    public List<Ztree> deptTreeData()
    {
        List<Ztree> ztrees = deptService.selectDeptTree(new SysDept());
        return ztrees;
    }

    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        mmap.put("dept", deptService.selectDeptById(deptId));
        return prefix + "/deptTree";
    }
}
